<?php
namespace app\content\controller;

use app\model\Classes;
use app\model\ClassPlan as ModelClassPlan;
use app\model\ClassPlanTask as ModelClassPlanTask;
use app\model\ClassUser;
use app\model\Exampaper;
use app\model\Product;
use app\model\ProductCourse;
use app\model\User;
use think\admin\Controller;
use think\admin\helper\QueryHelper;

/**
 * 学员学习计划详情
 * @class ClassPlan
 * @package app\content\controller
 */
class ClassPlanTask extends Controller
{
  
    /**
     * 学员学习计划详情
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        ModelClassPlanTask::mQuery()->layTable(function () {
            $this->title = '学习计划详情';
            $this->plan_list = ModelClassPlan::mk()->where(['status' => 1])->select();
            $this->product_list = Product::mk()->where(['status' => 1])->select();
        }, function (QueryHelper $query) {

            // 加载对应数据列表
            // $query->where(['is_deleted' => 0, 'status' => intval($this->type === 'index')]);

            // 关联用户身份资料
            /** @var \think\model\Relation|\think\db\Query $query */
            $query->with(['user' => static function ($query) {
                $query->field('id,name,sex,full_name,phone,image_path');
            }, 'plan' => static function ($query) {
                $query->field('id,name');
            }]);

            // 数据列表搜索过滤
            $query->equal('parent_id')->dateBetween('s_date');
            $query->like('name');
        });
    }

    /**
     * 添加学习计划详情
     * @auth true
     */
    public function add()
    {
        $this->plan_list = ModelClassPlan::mk()->where(['status' => 1])->select();
        $this->video_list = ProductCourse::mk()->where(['status' => 1])->field('id as value,name')->select();
        ModelClassPlanTask::mForm('form');
    }

    /**
     * 编辑学习计划详情
     * @auth true
     */
    public function edit()
    {
   
        $this->plan_list = ModelClassPlan::mk()->where(['status' => 1])->select();
        ModelClassPlanTask::mForm('form');
    }



    /**
     * 修改
     * @auth true
     */
    public function pass()
    {
        $this->_applyFormToken();
        if ($this->request->isGet()) {
            $this->verify = false;
            ModelClassPlanTask::mForm('pass');
        } else {
            $data = $this->_vali([
                'id.require'                  => '用户ID不能为空！',
             
            ]);
            $ModelClassPlan = ModelClassPlan::mk()->findOrEmpty($data['id']);
            if ($ModelClassPlan->isExists() && $ModelClassPlan->save([])) {
             
                $this->success('');
            } else {
                $this->error('');
            }
        }
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if(request()->isPost()){
            $plan_id = $this->request->post('parent_id');
            if($plan_id){
                $detail = ModelClassPlan::find($plan_id);
                $data['start_date'] = $detail->start_date;
                $data['end_date'] = $detail->end_date;
                $data['user_id']  = $detail->user_id;
            }
        }
    }

    /**
     * 修改学习计划详情状态
     * @auth true
     */
    public function state()
    {
        $this->_checkInput();
        ModelClassPlanTask::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除学习计划详情
     * @auth true
     */
    public function remove()
    {
        $this->_checkInput();
        ModelClassPlanTask::mDelete();
    }

    /**
     * 检查输入变量
     */
    private function _checkInput()
    {
       
    }

    //异步获取班级
    public function sys_get_class(){
        $product_id = $this->request->post('product_id');
        if($product_id){
            $list = Classes::mk()->where(['product_id' => $product_id])->field('id,name,product_id')->select();
            if(count($list)){
                $this->success('ok',$list);
            }else{
                $this->error('未找到班级信息');
            }
        }else{
            $this->error('参数错误');
        }
    }

    //异步获取学员
    public function sys_get_user(){
        $class_id = $this->request->post('class_id');
        if($class_id){
            $uids = ClassUser::mk()->where(['parent_id' => $class_id])->column('user_id');
            if(!count($uids)){
                $this->error('未找到学员信息');
            }
            $list = User::mk()->where('id','in',$uids)->field('id,name,full_name')->select();
            if(count($list)){
                $this->success('ok',$list);
            }else{
                $this->error('未找到班级信息');
            }
        }else{
            $this->error('参数错误');
        }
    }

  //异步获取计划
  public function sys_get_plan(){
        $plan_id = $this->request->post('plan_id');
        if($plan_id){
            $detail = ModelClassPlan::find($plan_id);
            $video_list = ProductCourse::mk()->where(['status' => 1])->where('product_id',$detail->product_id)->field('id as value,name')->select();
            $exampaper_list = Exampaper::mk()->where('product_id',$detail->product_id)->field('id as value,name')->select();
            if(!$detail){
                $this->error('未找到计划信息');
            }
            $this->success('ok',compact('detail','video_list','exampaper_list'));
        }else{
            $this->error('参数错误');
        }
    }

    
}
